package com.woniuxy.woniumall.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniumall.entity.User;
import com.woniuxy.woniumall.service.OrderServcie;
import com.woniuxy.woniumall.vo.OrderVo;
import com.woniuxy.woniumall.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("order")
public class OrderController {

    @Autowired
    OrderServcie orderServcie ;

    @PostMapping("/submitOrder")
    @ResponseBody
    public ResponseResult<Void> submitOrder(Integer aid , Integer[] ids, HttpSession session){
        ResponseResult<Void>  result = new ResponseResult<>();
        try {
            User user = (User) session.getAttribute("userInfo");
            orderServcie.submitOrder(aid,ids,user);
            result.setMsg("下单成功");
            result.setCode(200);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg(e.getMessage());
        }
        return result ;
    }

    @GetMapping("/list")
    @ResponseBody
    public ResponseResult<PageInfo<OrderVo>>  list(
                          @RequestParam(value = "now",defaultValue = "1") Integer now,
                          @RequestParam(value = "size",defaultValue = "2") Integer size,
                          int userid,String name){
        ResponseResult<PageInfo<OrderVo>> result = new ResponseResult<>();
        try {
            PageInfo<OrderVo> pageInfo = orderServcie.findOrderByPage(userid,name,now,size);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(pageInfo);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }



}
